import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int[] array = {1, 2, 3, 4};
            System.out.println(array[9]);

            int a = scanner.nextInt();
            System.out.println(a);
        } catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("捕获到空指针异常");
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("捕抓到数组越界异常");
        } finally {
            System.out.println("finally被执行");
        }
        System.out.println("after");
    }

    public static void main2(String[] args) {
        //如果具有父类子类关系的情况下，要把父类放到最后
        //runtimeexcep（父） NullPointerException（子）
        Scanner scanner = new Scanner(System.in);
        try {
            int[] array = {1,2,3,4};
            System.out.println(array[9]);

            int a = scanner.nextInt();
            System.out.println(a);
        }catch (NullPointerException e){ //只捕获了空指针异常，编译器给jvm处理，程序崩溃
            e.printStackTrace();
            System.out.println("捕获到空指针异常");
        }catch (ArrayIndexOutOfBoundsException e){ //现在可以捕抓数组越界异常了
            e.printStackTrace();
            System.out.println("捕抓到数组越界异常");
        }finally {
            System.out.println("finally被执行");
            //不管是否发生，都会被执行
            //finally作用：释放资源
            scanner.close();
        }
        System.out.println("after");
    }

    public static void func() throws NullPointerException{
        int[] array = null;
        System.out.println(array.length);
    }
    public static void main1(String[] args) {
        try {
            func();
        }catch (NullPointerException e){
            e.printStackTrace();  //打印栈上的信息
            System.out.println("空指针异常");
        }
        System.out.println("不管先，往下走");
    }
}
